package common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.dao.JdbcDAO;
import common.pojo.Review;


@Scope("prototype")
@Repository
public class ReviewDAO extends JdbcDAO {
	
	private final String TABLE="review";
	
	@SuppressWarnings("unchecked")
	public List<Review> find(String where,int start, int limit,String orderbys){
		 class Mapper implements RowMapper {
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				Review review = new Review();
				review.setId(rs.getInt(1));
				review.setOrderId(rs.getInt(2));
				review.setUserName(rs.getString(3));
				review.setEntryDate(rs.getTimestamp(4));
				review.setStatus(rs.getBoolean(5));
				review.setComment(rs.getInt(6));
				return review;
			}
		}
		String sql ="SELECT top "+limit+" id,orderId,userName,entryDate,status,comment "+
		" FROM (SELECT *,ROW_NUMBER() OVER (ORDER BY "+orderbys+") as rowNum "+
		"FROM "+TABLE+where+  
		") AS T "+ 
		"WHERE T.RowNum>"+start+ " order by rowNum ";
		return getList(sql, null, new Mapper());
	}
	
	@SuppressWarnings("unchecked")
	public List<Review> findList(String where,int start, int limit,String orderbys){
		String sql ="SELECT top "+limit+" id,orderId,userName,entryDate,status,comment,remark "+
		" FROM (SELECT *,ROW_NUMBER() OVER (ORDER BY "+orderbys+") as rowNum "+
		"FROM "+TABLE+" WHERE "+where+  
		") AS T "+ 
		"WHERE T.RowNum>"+start+ " order by rowNum ";
		return getList(sql, null, new ReviewMapper());
	}
	
	/**
	 * 取得数据总数
	 * @return
	 */
	public int getCount(String where){
		return getCount("select count(*) from "+TABLE+where,null);
	}	
	/**
	 * 根据id操作
	 * @param id
	 * @return
	 */
	public Review find(int id){

		String sql = "select id,orderId,userName,entryDate,status,comment,remark " +
		"from "+TABLE+" where id ="+id;
		return (Review) getObject(sql,null, new ReviewMapper());
	}
	

	
	public void verify(int id){
		update("update "+TABLE+" set status=1 where id="+id);
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */ 		
	public boolean delete(int id){
		String sql ="delete from "+TABLE+" where id = "+id;
		delete(sql);
		return true;
	}
	
	@SuppressWarnings("unchecked")
	public List<Review> findList(String where){
		String sql ="SELECT  id,orderId,userName,entryDate,status,comment,remark  from "+TABLE+" where "+where;
		return getList(sql, null, new ReviewMapper());
	}
	
	private static final class ReviewMapper implements RowMapper {
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Review review = new Review();
			review.setId(rs.getInt(1));
			review.setOrderId(rs.getInt(2));
			review.setUserName(rs.getString(3));
			review.setEntryDate(rs.getTimestamp(4));
			review.setStatus(rs.getBoolean(5));
			review.setComment(rs.getInt(6));
			review.setRemark(rs.getString(7));
			return review;
		}
	}
	


	public void save(int orderid, Review review, String userName) {
		String sql="insert into "+TABLE+" values(?,?,getdate(),0,?,?)";
		save(sql, new Object[]{orderid,userName,review.getComment(),review.getRemark()});
	}

	
}
